package com.web.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opencsv.CSVWriter;
import com.web.common.pojos.Car;
import com.web.common.vo.TotalSaleCounts;
import com.web.mapper.CarMapper;
import com.web.service.Task1Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/6/7 上午11:32]
 */
@Service
@Slf4j
public class Task1ServiceImpl extends ServiceImpl<CarMapper, Car> implements Task1Service {
    int key=0;
    // 文件路径及名称
    String filePath = "D:\\JAVAwork\\myself_idea_work\\cai-view\\car_view_web\\src\\main\\java\\com\\web\\data\\car_sales_data.csv";
    @Autowired
    private CarMapper carMapper;

    @Override
    public TotalSaleCounts getTotalSaleCounts(String algorithmName) {
        //实际
        List<Integer> dates = carMapper.selectDate();
        log.info("data:{}",dates.toString());
        List<Integer> saleCounts = carMapper.selectCountByDate();
        log.info("sale_count{}",saleCounts.toString());
        if (key==0)
        {
            writeDataToCsv(dates,saleCounts);
        }
        List<String> datesAsString = dates.stream()
                .map(date -> {
                    String year = String.format("%04d", date / 100);
                    String month = String.format("%02d", date % 100);
                    return year + "年" + month + "月";
                })
                .collect(Collectors.toList());
        //预测
        //TODO
        List<String> predictDates = new ArrayList<>();
        for (int i =1;i<=6;i++)
        {
            predictDates.add("2024年"+(4+i)+"月");
        }
        List<Integer> predictSaleCounts = new ArrayList<>();
        Integer[] LSTMMoreList = {603894,597436,418986,620658,609851,603593};
        Integer[] LSTMOneList = {603242,633132,696865,686754,847383,605703};
        Integer[] GMList = {649750,817941,841494,959662,1079363,1176817};
        if (algorithmName.equals("1"))
        {
            predictSaleCounts.addAll(List.of(LSTMOneList));
        }else if (algorithmName.equals("2"))
        {
            predictSaleCounts.addAll(List.of(LSTMMoreList));
        }else if (algorithmName.equals("3"))
        {
            predictSaleCounts.addAll(List.of(GMList));
        }


        datesAsString.addAll(predictDates);
        saleCounts.addAll(predictSaleCounts);
        TotalSaleCounts totalSaleCounts = new TotalSaleCounts();
        totalSaleCounts.setDates(datesAsString);
        totalSaleCounts.setSaleCounts(saleCounts);
        return totalSaleCounts;
    }
    public void writeDataToCsv(List<Integer> dates, List<Integer> saleCounts) {
        try {
            // 创建FileWriter对象
            FileWriter writer = new FileWriter(filePath);

            // 创建CSVWriter对象
            CSVWriter csvWriter = new CSVWriter(writer);

            // 写入表头
            String[] header = {"Date", "Sale Count"};
            csvWriter.writeNext(header);

            // 确保两个列表长度相同，避免索引越界
            int size = Math.min(dates.size(), saleCounts.size());

            // 遍历列表，写入数据
            for (int i = 0; i < size; i++) {
                String[] data = {String.valueOf(dates.get(i)), String.valueOf(saleCounts.get(i))};
                csvWriter.writeNext(data);
            }

            // 关闭CSVWriter
            csvWriter.close();
            key =1;
            log.info("Data has been written to CSV file successfully.");
        } catch (IOException e) {
            log.error("Error occurred while writing to CSV file: ", e);
        }
    }
}
